﻿using System;
using System.Windows.Controls;
using KYKJ.Monitor.APIs;

namespace KYKJ.Monitor.Video
{
    /// <summary>
    /// SettingsControl.xaml 的交互逻辑
    /// </summary>
    public partial class CameraSet : UserControl
    {
        #region 字段

        private System.Threading.Thread m_thread;

        private bool isMouseDown;

        private object m_lockObj = new object();

        private VideoMonitorControl m_parent;

        #endregion

        #region 构造函数

        public CameraSet()
        {
            InitializeComponent();
        }

        #endregion

        #region 焦点控制

        private void btnFocalDistanceAdd_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzZoomInStart((uint)this.sldSpeed.Value);
        }

        private void btnFocalDistanceAdd_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzZoomInEnd((uint)this.sldSpeed.Value);
        }

        private void btnFocalDistanceAdd_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzZoomInEnd((uint)this.sldSpeed.Value);
        }

        private void btnFocalDistanceSub_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzZoomOutStart((uint)this.sldSpeed.Value);
        }

        private void btnFocalDistanceSub_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzZoomOutEnd((uint)this.sldSpeed.Value);
        }

        private void btnFocalDistanceSub_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzZoomOutEnd((uint)this.sldSpeed.Value);
        }

        private void btnFocusingAdd_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzFoucsNearStart((uint)this.sldSpeed.Value);
        }

        private void btnFocusingAdd_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzFoucsNearEnd((uint)this.sldSpeed.Value);
        }

        private void btnFocusingAdd_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzFoucsNearEnd((uint)this.sldSpeed.Value);
        }

        private void btnFocusingSub_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzFoucsFarStart((uint)this.sldSpeed.Value);
        }

        private void btnFocusingSub_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzFoucsFarEnd((uint)this.sldSpeed.Value);
        }

        private void btnFocusingSub_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzFoucsFarEnd((uint)this.sldSpeed.Value);
        }

        private void btnApertureAdd_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzIrisOpenStart((uint)this.sldSpeed.Value);
        }

        private void btnApertureAdd_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzIrisOpenEnd((uint)this.sldSpeed.Value);
        }

        private void btnApertureAdd_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzIrisOpenEnd((uint)this.sldSpeed.Value);
        }

        private void btnApertureSub_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzIrisCloseStart((uint)this.sldSpeed.Value);
        }

        private void btnApertureSub_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzIrisCloseEnd((uint)this.sldSpeed.Value);
        }

        private void btnApertureSub_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzIrisCloseEnd((uint)this.sldSpeed.Value);
        }

        #endregion

        #region 预置信息和外部设备控制

        private void btnLight_Click(object sender, System.Windows.RoutedEventArgs e)
        {

        }

        private void btnWiper_Click(object sender, System.Windows.RoutedEventArgs e)
        {

        }

        private void btnPosition_Click(object sender, System.Windows.RoutedEventArgs e)
        {

        }

        private void btnTrajectory_Click(object sender, System.Windows.RoutedEventArgs e)
        {

        }

        #endregion

        #region 图像属性参数设置

        private void btnReSet_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.pbBrightness.Value = 50;
            this.pbContrast.Value = 50;
            this.pbSaturation.Value = 50;
            this.pbTone.Value = 50;
            //发送控制命令
        }
        
        private void btnBrightnessAdd_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = true;
            }
            m_thread = new System.Threading.Thread(() =>
            {
                while (isMouseDown)
                {
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        this.pbBrightness.Value += 1;
                        Console.WriteLine(this.pbBrightness.Value);
                        if (Math.Abs(this.pbBrightness.Value - this.pbBrightness.Maximum) < 0.1) isMouseDown = false;
                    }));
                    //发送控制开始命令
                    System.Threading.Thread.Sleep(100);
                }
            });
            m_thread.IsBackground = true;
            m_thread.Start();
        }

        private void btnBrightnessAdd_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = false;
            }
            //发送控制停止命令
        }

        private void btnBrightnessSub_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = true;
            }
            m_thread = new System.Threading.Thread(() =>
            {
                while (isMouseDown)
                {
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        this.pbBrightness.Value -= 1;
                        if (Math.Abs(this.pbBrightness.Value - this.pbBrightness.Minimum) < 0.1) isMouseDown = false;
                    }));
                    //发送控制开始命令
                    System.Threading.Thread.Sleep(100);
                }
            });
            m_thread.IsBackground = true;
            m_thread.Start();
        }

        private void btnBrightnessSub_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = false;
            }
            //发送控制停止命令
        }

        private void btnContrastAdd_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = true;
            }
            m_thread = new System.Threading.Thread(() =>
            {
                while (isMouseDown)
                {
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        this.pbContrast.Value += 1;
                        if (Math.Abs(this.pbContrast.Value - this.pbContrast.Maximum) < 0.1) isMouseDown = false;
                    }));
                    //发送控制开始命令
                    System.Threading.Thread.Sleep(100);
                }
            });
            m_thread.IsBackground = true;
            m_thread.Start();
        }

        private void btnContrastAdd_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = false;
            }
            //发送控制停止命令
        }

        private void btnContrastSub_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = true;
            }
            m_thread = new System.Threading.Thread(() =>
            {
                while (isMouseDown)
                {
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        this.pbContrast.Value -= 1;
                        if (Math.Abs(this.pbContrast.Value - this.pbContrast.Minimum) < 0.1) isMouseDown = false;
                    }));
                    //发送控制开始命令
                    System.Threading.Thread.Sleep(100);
                }
            });
            m_thread.IsBackground = true;
            m_thread.Start();
        }

        private void btnContrastSub_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = false;
            }
            //发送控制停止命令
        }

        private void btnSaturationAdd_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = true;
            }
            m_thread = new System.Threading.Thread(() =>
            {
                while (isMouseDown)
                {
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        this.pbSaturation.Value += 1;
                        if (Math.Abs(this.pbSaturation.Value - this.pbSaturation.Maximum) < 0.1) isMouseDown = false;
                    }));
                    //发送控制开始命令
                    System.Threading.Thread.Sleep(100);
                }
            });
            m_thread.IsBackground = true;
            m_thread.Start();
        }

        private void btnSaturationAdd_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = false;
            }
            //发送控制停止命令
        }

        private void btnSaturationSub_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = true;
            }
            m_thread = new System.Threading.Thread(() =>
            {
                while (isMouseDown)
                {
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        this.pbSaturation.Value -= 1;
                        if (Math.Abs(this.pbSaturation.Value - this.pbSaturation.Minimum) < 0.1) isMouseDown = false;
                    }));
                    //发送控制开始命令
                    System.Threading.Thread.Sleep(100);
                }
            });
            m_thread.IsBackground = true;
            m_thread.Start();
        }

        private void btnSaturationSub_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = false;
            }
            //发送控制停止命令
        }

        private void btnToneAdd_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = true;
            }
            m_thread = new System.Threading.Thread(() =>
            {
                while (isMouseDown)
                {
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        this.pbTone.Value += 1;
                        if (Math.Abs(this.pbTone.Value - this.pbTone.Maximum) < 0.1) isMouseDown = false;
                    }));
                    //发送控制开始命令
                    System.Threading.Thread.Sleep(100);
                }
            });
            m_thread.IsBackground = true;
            m_thread.Start();
        }

        private void btnToneAdd_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = false;
            }
            //发送控制停止命令
        }

        private void btnToneSub_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = true;
            }
            m_thread = new System.Threading.Thread(() =>
            {
                while (isMouseDown)
                {
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        this.pbTone.Value -= 1;
                        if (Math.Abs(this.pbTone.Value - this.pbTone.Minimum) < 0.1) isMouseDown = false;
                    }));
                    //发送控制开始命令
                    System.Threading.Thread.Sleep(100);
                }
            });
            m_thread.IsBackground = true;
            m_thread.Start();
        }

        private void btnToneSub_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = false;
            }
            //发送控制停止命令
        }

        private void button_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            lock (m_lockObj)
            {
                isMouseDown = false;
            }
            //发送控制停止命令
        }

        #endregion

        #region 云台控制

        private void buttonCenter_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzAuto((uint)this.sldSpeed.Value);
        }

        private void buttonUp_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzUpStart((uint)this.sldSpeed.Value);
        }

        private void buttonUp_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzUpEnd((uint)this.sldSpeed.Value);
        }

        private void buttonUp_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzUpEnd((uint)this.sldSpeed.Value);
        }

        private void btnRightUp_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzRightUpStart((uint)this.sldSpeed.Value);
        }

        private void btnRightUp_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzRightUpEnd((uint)this.sldSpeed.Value);
        }

        private void btnRightUp_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzRightUpEnd((uint)this.sldSpeed.Value);
        }

        private void btnRight_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzRightStart((uint)this.sldSpeed.Value);
        }

        private void btnRight_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzRightEnd((uint)this.sldSpeed.Value);
        }

        private void btnRight_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzRightEnd((uint)this.sldSpeed.Value);
        }

        private void btnRightDown_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzRightDownStart((uint)this.sldSpeed.Value);
        }

        private void btnRightDown_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzRightDownEnd((uint)this.sldSpeed.Value);
        }

        private void btnRightDown_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzRightDownEnd((uint)this.sldSpeed.Value);
        }

        private void btnDown_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzDownStart((uint)this.sldSpeed.Value);
        }

        private void btnDown_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzDownEnd((uint)this.sldSpeed.Value);
        }

        private void btnDown_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzDownEnd((uint)this.sldSpeed.Value);
        }

        private void btnLeftDown_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzLeftDownStart((uint)this.sldSpeed.Value);
        }

        private void btnLeftDown_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzLeftDownEnd((uint)this.sldSpeed.Value);
        }

        private void btnLeftDown_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzLeftDownEnd((uint)this.sldSpeed.Value);
        }

        private void btnLeft_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzLeftStart((uint)this.sldSpeed.Value);
        }

        private void btnLeft_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzLeftEnd((uint)this.sldSpeed.Value);
        }

        private void btnLeft_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzLeftEnd((uint)this.sldSpeed.Value);
        }

        private void btnLeftUp_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzLeftUpStart((uint)this.sldSpeed.Value);
        }

        private void btnLeftUp_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzLeftUpEnd((uint)this.sldSpeed.Value);
        }

        private void btnLeftUp_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (VideoMonitorControl.InitState < 1) return;
            PTZControl.PtzLeftUpEnd((uint)this.sldSpeed.Value);
        }

        #endregion

        #region 私有方法

        private void SetTrackSize_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            m_parent.TrackWidth = Convert.ToInt32(this.txtTrackWidth.Text);
            m_parent.TrackHeight = Convert.ToInt32(this.txtTrackHeight.Text);
        }

        #endregion

        #region 公共方法

        public void SetParent(VideoMonitorControl parent)
        {
            m_parent = parent;
        }

        public void Close()
        {
            PTZControl.Close();
        }

        #endregion

        private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (VideoMonitorControl.InitState==0)
            {
                VideoMonitorControl.InitState = PTZControl.Init("192.168.0.66",8877,"admin","kykj123456",1);
            }
        }
    }
}
